﻿using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.Model;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using Microsoft.AspNetCore.Mvc;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.Permissions
{
    /// <summary>
    /// 按钮管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ActionController : BaseController
    {
        private readonly SimpleRepositroy<TActions> actionrepost;
        private readonly SimpleRepositroy<TRoleMenuActions> rolemenuactionrepost;
        private readonly SimpleRepositroy<TMenuActions> menuactionrepost;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="actionrepost"></param>
        public ActionController(SimpleRepositroy<TActions> actionrepost
            )
        {
            this.actionrepost = actionrepost;
            rolemenuactionrepost = actionrepost.ChangeRepository<SimpleRepositroy<TRoleMenuActions>>();
            menuactionrepost = actionrepost.ChangeRepository<SimpleRepositroy<TMenuActions>>();
        }
        /// 
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddAction(TActions m)
        {
            var action = actionrepost.GetFirst(a=>a.ActionCode == m.ActionCode);
            if (action != null)
            {
                return Failed("编码已存在");
            }
            if (!actionrepost.Insert(m))
            {
                return Failed("添加失败");
            }
            return Success("添加成功");
        }

        /// <summary>
        /// 显示按钮列表
        /// </summary>
        /// <param name="aname"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetActionList(string? aname = "", int status = 2, int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Failed("分页条件有误");
            }
            var actionlist = actionrepost.GetList();
            var count = actionlist.Count;
            if (!string.IsNullOrEmpty(aname))
            {
                actionlist = actionlist.Where(a => a.ActionName.Contains(aname)).ToList();
            }
            if (status <= 1)
            {
                actionlist = actionlist.Where(a=>a.Status == status).ToList();
            }
            actionlist = actionlist.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return Success(actionlist, count, "查询成功");

        }


        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAction(int aid) 
        {
            if (aid <= 0)
            {
                return Failed("Id有误");
            }
            var action = actionrepost.GetFirst(a=>a.Id == aid);
            if (action is null)
            {
                return Failed("未查出此按钮");
            }
            
            try
            {
                actionrepost.AsTenant().BeginTran();
                if (!actionrepost.Delete(action))
                {
                    actionrepost.AsTenant().RollbackTran();
                    return Failed("删除按钮失败");
                }
                var rmalist = rolemenuactionrepost.GetList(r => r.ActionId == aid);
                foreach (var rma in rmalist)
                {
                    if (!rolemenuactionrepost.Delete(rma))
                    {
                        actionrepost.AsTenant().RollbackTran();
                        return Failed("删除角色菜单操作中的按钮失败");
                    }
                }
                var malist = menuactionrepost.GetList(m=>m.ActionId == aid);
                foreach (var ma in malist)
                {
                    if (!menuactionrepost.Delete(ma))
                    {
                        actionrepost.AsTenant().RollbackTran();
                        return Failed("删除角色菜单操作中的按钮失败");
                    }
                }
                actionrepost.AsTenant().CommitTran();
                return Success("删除成功");
            }
            catch (Exception ex)
            {
                actionrepost.AsTenant().RollbackTran();
                return Failed(ex.Message);
                throw;
            }
           
        }


        /// <summary>
        /// 根据主键获取信息
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ActionById(int aid)
        {
            if (aid <= 0)
            {
                return Failed("Id有误!");
            }
            var obj = actionrepost.GetFirst(a=> a.Id == aid);
            if (obj == null)
            {
                return Failed("查询失败!");
            }
            return Success(obj, "查询成功!");
        }

        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdAction(TActions m)
        {
            if (m.Id <= 0)
            {
                return Failed("Id有误!");
            }
            var action = actionrepost.GetFirst(a=>a.Id != m.Id && a.ActionCode == m.ActionCode);
            if (action != null)
            {
                return Failed("修改失败！已存在该操作编码！");
            }
            m.ModifyDate = DateTime.Now;
            if (!actionrepost.Update(m))
            {
                return Failed("修改失败！");
            }
            return Success("修改成功");
        }
    }
}
